home *** CD-ROM | disk | FTP | other *** search
- #ifndef _GameUtilities_h_
- #define _GameUtilities_h_
-
- #include "GameTypes.h"
-
- #define abs( x ) ( x > 0 ? x : -x )
-
-
- //tOwnership SetOwner( char individual, char team );
- Boolean GetTextFromUser( char *message , char *text);
-
- Boolean CStrEqual( char *str1 , char *str2 );
-
- #define rToR( foo ) (*(Rect *)(& foo))
- #define RTor( foo ) (*(rect *)(& foo))
-
- // templates
-
- /*----------------------------------------------------------------------------\
-
- SectPtRect
-
- \----------------------------------------------------------------------------*/
- template <class P , class R >
- Boolean SectPtRect( P &thePt, R &therect )
- {
-
- if( ( thePt.x >= therect.left ) && ( thePt.x < therect.right ) )
- if( ( thePt.y >= therect.top ) && ( thePt.y < therect.bottom ) )
- return( true );
-
- return( false );
-
- }
-
- /*----------------------------------------------------------------------------\
-
- MySectrect
- -- checks to see if two rects intersect each toher
- \----------------------------------------------------------------------------*/
-
- template <class R >
- Boolean MySectRect( const R *firstrect, const R *secrect )
- {
- if( firstrect->right <= secrect->left )
- return false;
-
- if( firstrect->left >= secrect->right )
- return false;
-
- if( firstrect->bottom <= secrect->top )
- return false;
-
- if( firstrect->top >= secrect->bottom )
- return false;
-
- return true;
- }
-
- /*----------------------------------------------------------------------------\
-
- MyCropRect
-
- - true = it was in side or partly in side
- - false = the two rects did not intersect at all
- a = the rect that is being croped
- b = the rect that is croping
- c = the croped rect
-
- \----------------------------------------------------------------------------*/
-
- template <class R >
- Boolean MyCropRect( const R *a, const R *b , R *c )
- {
-
- if( !MySectRect( a , b ) )
- return false;
-
- if( a->left < b->left )
- c->left = b->left;
- else
- c->left = a->left;
-
- if( a->right > b->right )
- c->right = b->right;
- else
- c->right = a->right;
-
- if( a->top < b->top )
- c->top = b->top;
- else
- c->top = a->top;
-
- if( a->bottom > b->bottom )
- c->bottom = b->bottom;
- else
- c->bottom = a->bottom;
-
- return true;
- }
-
- /*----------------------------------------------------------------------------\
-
- RectDistance
-
- - how far a rect is from each other - intersect = 0
- - note the distance is squared
-
- \----------------------------------------------------------------------------*/
-
- template <class R >
- ulong RectDistance( const R &rectA, const R &rectB )
- {
-
- if( !MySectRect( &rectA , &rectB ) )
- {
-
- // check to see if it is directly above or below
- if(( rectB.left >= rectA.left) && (rectB.right <= rectA.right ) ||
- (rectB.left <= rectA.left )&& (rectB.right > rectA.left) ||
- (rectB.right >= rectA.right )&& (rectB.left < rectA.right) )
- {
- if( rectB.bottom < rectA.top )
- return( rectA.top - rectB.bottom );
- else
- return( rectB.top - rectA.bottom );
- }
- // see if it is to the left or right
- if(( rectB.top >= rectA.top) && (rectB.bottom <= rectA.bottom ) ||
- (rectB.top <= rectA.top )&& (rectB.bottom > rectA.top) ||
- (rectB.bottom >= rectA.bottom )&& (rectB.top < rectA.bottom) )
- {
- if( rectB.right < rectA.left )
- return( rectA.left - rectB.right );
- else
- return( rectB.left - rectA.right );
- }
- // handle it if they are diagonal of each other now
- short x,y;
-
- x = rectA.left - rectB.right;
- y = rectA.top - rectB.bottom;
-
- return( x * x + y * y );
- }
-
- return 0;
- }
-
- /*----------------------------------------------------------------------------\
-
- MySetRect
-
- set the rect giving each point
- \----------------------------------------------------------------------------*/
-
- template <class R , class S >
- void MySetRect( R *theRect , S l , S t , S r , S b )
- {
- theRect->left = l;
- theRect->right = r;
- theRect->top = t;
- theRect->bottom = b;
- }
-
- /*----------------------------------------------------------------------------\
-
- MySetRectWH
-
- Set the rect using widths and heights
- \----------------------------------------------------------------------------*/
-
- template <class R , class S >
- void MySetRectWH( R *theRect , S l , S t , S width , S height )
- {
- theRect->left = l;
- theRect->right = l + width;
- theRect->top = t;
- theRect->bottom = t + height;
- }
-
- /*----------------------------------------------------------------------------\
-
- MyOffSetRect
-
- change rects pos by
- \----------------------------------------------------------------------------*/
-
- template <class R , class S >
- void MyOffSetRect( R *theRect , S x , S y )
- {
- theRect->left += x;
- theRect->right += x;
- theRect->top += y;
- theRect->bottom += y;
- }
- #endif